package com.lkpstudy.rbac.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lkpstudy.rbac.base.BaseController;
import com.lkpstudy.rbac.entity.Auth;
import com.lkpstudy.rbac.entity.Role;
import com.lkpstudy.rbac.entity.User;
import com.lkpstudy.rbac.param.UserEditPlusParam;
import com.lkpstudy.rbac.service.UserService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author likaiping
 * @editTime 2024/4/17 20:31
 */
//
@RestController
@RequestMapping("/user")
public class UserController extends BaseController<UserService,User> {

    @GetMapping("/info")
    public User get(@RequestHeader("username") String username) {
        if (Objects.isNull(username)) {
            throw new RuntimeException("用户名不能为空");
        }
        /* 第一个参数"username"是数据库中的字段 */
        return baseService.getOne(new QueryWrapper<User>().eq("username",username));
    }

    @GetMapping("/info/roles")
    public List<Role> findRoles(@RequestHeader("username") String username) {
        return baseService.findRoles(username);
    }

    @GetMapping("/info/auths")
    public List<Auth> findAuths(@RequestHeader("username") String username) {
        return baseService.findAuths(username);
    }

    @PutMapping("/edit/roles")
    public boolean editRoles(@RequestHeader("username") String username,@RequestBody List<Long> roleIds) {
        return baseService.editRoles(username,roleIds);
    }
}
